package com.rtx.edi.platform.express.auth.service.impl;

import java.util.HashMap;
import java.util.UUID;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.rtx.edi.platform.express.auth.service.TokenService;
import com.taobao.api.ApiException;
import com.taobao.api.DefaultTaobaoClient;
import com.taobao.api.TaobaoClient;
import com.taobao.api.request.TopAuthTokenCreateRequest;
import com.taobao.api.response.TopAuthTokenCreateResponse;

@Service
public class TokenServiceImpl implements TokenService {
	
	private Logger logger = LoggerFactory.getLogger(this.getClass());
	
	public static HashMap<String, Object> tokenMap= new HashMap<String, Object>();
	
	
	@Override
	public String getTbtoken(String getway, String appkey, String appsecret, String authcode) throws ApiException {
		String token="";
		// 调用淘宝sdk获取token 
		Long current=System.currentTimeMillis();
		if(tokenMap.get("TB|"+appkey+"|access_token")==null) {
			token = getIniTbtoken(getway, appkey, appsecret, authcode);
		}else if(tokenMap.get("TB|"+appkey+"|access_token")!=null &&
				tokenMap.get("TB|"+appkey+"|expire_time")!=null 
				&& current-Long.parseLong(tokenMap.get("TB|"+appkey+"|expire_time").toString())>0){
			//  token 失效了 
			token = getIniTbtoken(getway, appkey, appsecret, authcode);
		}else {
			token=tokenMap.get("TB|"+appkey+"|access_token").toString();
		}
		
		
		return token;
	}
	


	@Override
	public String getDytoken(String getway, String appkey, String appsecret, String authcode) {
		

		return null;
	}

	@Override
	public String getPddtoken(String getway, String appkey, String appsecret, String authcode) {
		
		
		return null;
	}

	@Override
	public String getJdtoken(String getway, String appkey, String appsecret, String authcode) {
		

		return null;
	}

	@Override
	public String getXhstoken(String getway, String appkey, String appsecret, String authcode) {
		

		return null;
	}

	@Override
	public String getDwtoken(String getway, String appkey, String appsecret, String authcode) {
	
		
		return null;
	}
	
	/**
	 *     调用淘宝接口获取token
	 * @param getway
	 * @param appkey
	 * @param appsecret
	 * @param authcode
	 * @return
	 * @throws ApiException
	 */
	private String getIniTbtoken(String getway, String appkey, String appsecret, String authcode) throws ApiException {
		TaobaoClient client = new DefaultTaobaoClient(getway, appkey, appsecret);
		 TopAuthTokenCreateRequest req = new TopAuthTokenCreateRequest();
		 req.setCode(authcode);
		 req.setUuid(UUID.randomUUID().toString());
		 TopAuthTokenCreateResponse rsp = client.execute(req);
		 JSONObject json=JSON.parseObject(rsp.getTokenResult());
		 String token=json.getString("access_token");
		 String refreshtoken=json.getString("refresh_token");
		 Long expiretime=json.getLong("expire_time");
		 Long refreshtokenvalidtime=json.getLong("refresh_token_valid_time");
		 
		 tokenMap.put("TB|"+appkey+"|access_token",token);
		 // tokenMap.put("TB|"+appkey+"|refresh_token",refreshtoken);
		 tokenMap.put("TB|"+appkey+"|expire_time",expiretime);
		 // tokenMap.put("TB|"+appkey+"|refresh_token_valid_time",refreshtokenvalidtime);
		return token;
	}

}
